<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyMud - 游戏主界面</title>
    <link rel="stylesheet" href="/static/css/style.css">
    <link rel="stylesheet" href="/static/css/Game.css">
</head>
<body>
    <div class="game-container">
        <div class="game-header">
            <h1>欢迎来到MyMud游戏世界！</h1>
            <button id="logout-btn" class="btn btn-danger">退出登录</button>
        </div>
        
        <div class="game-content">
            <div class="game-info">
                <h2>游戏状态</h2>
                <p>您已成功登录并进入游戏世界</p>
                <p>登录凭证：<span id="cookie-info">检查中...</span></p>
            </div>
            
            <div class="game-actions">
                <h2>游戏操作</h2>
                <p>游戏功能正在开发中...</p>
            </div>
        </div>
    </div>

    <script src="/static/js/axios.min.js"></script>
    <script>
        // 检查cookie状态
        function checkCookie() {
            // 不直接读取Cookie，而是发送请求到后端验证登录状态
            axios.get('/api/check-login')
                .then(res => {
                    if (res.data.loggedIn) {
                        document.getElementById('cookie-info').textContent = `登录状态有效`;
                    } else {
                        document.getElementById('cookie-info').textContent = `登录状态无效`;
                        alert('登录状态丢失，请重新登录');
                        window.location.href = '/';
                    }
                })
                .catch(error => {
                    console.error('检查登录状态时出错:', error);
                    document.getElementById('cookie-info').textContent = `检查失败`;
                });
        }

        // 退出登录功能
        function handleLogout() {
            // 调用后端API来清除Cookie（HttpOnly Cookie只能由服务器清除）
            axios.post('/api/logout')
                .then(res => {
                    alert('已成功退出登录');
                    // 根据后端返回的重定向路径跳转
                    if (res.data.redirect) {
                        window.location.href = res.data.redirect;
                    }
                })
                .catch(error => {
                    console.error('退出登录时出错:', error);
                    alert('退出登录失败，请重试');
                });
        }

        // 页面加载时执行
        document.addEventListener('DOMContentLoaded', () => {
            checkCookie();
            document.getElementById('logout-btn').addEventListener('click', handleLogout);
        });
    </script>
</body>
</html>